<?php

if (!defined('BASEPATH'))
    exit('Akses langsung tidak diperkenankan');

/**
 * class Registration
 * menyediakan fungsi2 untuk registrasi/pendaftaran user, meliputi:
 *      1. Mendaftarkan data user ke database
 *      2. Validasi masukan user
 *
 * @author faiqmiftakhul
 */
class Registration extends CI_Controller {

    /** _public_view sebagai wrapper header-content-footer */
    var $_public_view;

    /** array header untuk membungkus data header apa saja yang di render */
    var $header;

    /**
     * Konstruktor standar yang meload database, model, library, dan konfigurasi yang diperlukan
     */
    public function __construct() {
        parent::__construct();
        $this->load->database();
        $this->load->helper('url');
        $this->load->library('form_validation');
        $this->load->library('session');
        $this->config->load('artyproperty');
        $this->_public_view = $this->config->item('public_view');
    }

    public function index() {
        if ($this->input->get('paket', TRUE) >= "1" && $this->input->get('paket', TRUE) <= "4") {
            $data = array('page_content' => 'form',
                'page_title' => 'Isi Formulir',
                'isLogin' => FALSE);
        } else {
            $data = array('page_content' => 'reg',
                'page_title' => 'Pilih Paket',
                'isLogin' => FALSE);
        }
        $this->load->view($this->_public_view, $data);
    }

    /**
     * fungsi untuk mendaftarkan data user ke database sistem
     */
    public function reg_user() {
        $this->form_validation->set_rules('perusahaan_id', 'Perusahaan', 'required');
        $this->form_validation->set_rules('title', 'Titel', 'required');
        $this->form_validation->set_rules('nama_depan', 'Nama depan', 'required');
        $this->form_validation->set_rules('nama_belakang', 'Nama belakang', 'required');
        $this->form_validation->set_rules('birthday', 'Ulang tahun', 'required');
        $this->form_validation->set_rules('email', 'Alamat email', 'required|valid_email|callback_email_check');
        $this->form_validation->set_rules('username', 'Username', 'required|callback_username_check');
        $this->form_validation->set_rules('no_hp', 'Nomor ponsel', 'required|numeric');
        $this->form_validation->set_rules('no_telpon', 'Nomor telepon', 'required|numeric');
        $this->form_validation->set_rules('fax', 'Nomor fax', 'required|numeric');
        $this->form_validation->set_rules('pin_bb', 'Blackberry PIN', 'alpha_numeric|exact_length[8]');
        $this->form_validation->set_rules('provinsi', 'Propinsi', 'required');
        $this->form_validation->set_rules('password', 'Kata Sandi', 'required|min_length[8]|alpha_numeric|matches[password_confirm]');
        $this->form_validation->set_rules('password_confirm', 'Konfirmasi Kata Sandi', 'required');

        if ($this->form_validation->run() == FALSE) {
            $data = array('page_content' => 'form',
                'page_title' => 'Isi Formulir',
                'isLogin' => FALSE);
        } else {
            //ambil dari method POST
            $reg['perusahaan_id'] = 3; //$this->input->post('perusahaan_id', TRUE);
            $reg['job_title'] = $this->input->post('job_title', TRUE);
            $reg['title'] = $this->input->post('title', TRUE);
            $reg['nama_depan'] = $this->input->post('nama_depan', TRUE);
            $reg['nama_belakang'] = $this->input->post('nama_belakang', TRUE);
            //$reg['tgl_lahir'] = $this->input->post('birthday', TRUE);
            $reg['email'] = $this->input->post('email', TRUE);
            $reg['username'] = $this->input->post('username', TRUE);
            $reg['mobile_country'] = $this->input->post('mobile_country', TRUE);
            $reg['no_hp'] = $this->input->post('no_hp', TRUE);
            $reg['no_telpon'] = $this->input->post('no_telpon', TRUE);
            $reg['fax'] = $this->input->post('fax', TRUE);
            $reg['pin_bb'] = $this->input->post('pin_bb', TRUE);
            $alamat['provinsi'] = $this->input->post('provinsi', TRUE);
            $alamat['kota_kab'] = $this->input->post('kota_kab', TRUE);
            $reg['password'] = md5($this->input->post('password', TRUE));
            $password_confirm = md5($this->input->post('password_confirm', TRUE));
            $reg['profil'] = $this->input->post('profil', TRUE);
            $reg['pengalaman'] = $this->input->post('pengalaman', TRUE);
            $reg['payment'] = $this->input->post('payment', TRUE);

            $reg['level'] = 2;  //default level di set 1
            $reg['alamat_id'] = 3;  //ini dummy default alamatnya di 3 (nanti harus berdasar input ya)
            $reg['bahasa_id'] = 1;
            $reg['spesialisasi_prop_id'] = 1;

            $this->db->trans_begin();
            $this->db->insert('user', $reg);
            if ($this->db->trans_status() == FALSE) {
                $this->db->trans_rollback();
            } else {
                $this->db->trans_commit();
                $data = array('page_content' => 'reg_success',
                    'page_title' => 'Registrasi Berhasil',
                    'level_user' => $reg['level'],
                    'isLogin' => FALSE,
                    'note' => '');
            }
        }
        $this->load->view($this->_public_view, $data);  //tampilkan datanya
    }

    function email_check($str) {
        $sql = "SELECT email FROM user WHERE email='" . $str . "'";
        $query = $this->db->query($sql);
        $this->form_validation->set_message('email_check', "Email $str sudah ada. Silakan isi dengan email yang lain");
        return ($query->num_rows() == 0) ? TRUE : FALSE;
    }

    function username_check($str) {
        $sql = "SELECT username FROM user WHERE username='" . $str . "'";
        $query = $this->db->query($sql);
        $this->form_validation->set_message('username_check', "Nama $str sudah ada. Silakan isi dengan username yang lain");
        return ($query->num_rows() == 0) ? TRUE : FALSE;
    }

}

?>
